package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.CustomerReportQuery;
import cn.wolfcode.crm.service.ICustomerReportService;
import cn.wolfcode.crm.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by GG on 2020/3/27.
 */
@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportService;
    @RequestMapping("/list")
    public String list(Model model , @ModelAttribute("qo")CustomerReportQuery qo){
        model.addAttribute("result",customerReportService.selectCustomerReport(qo));
        return "customerReport/list";
    }

    @RequestMapping("/listByBar")
    public String listByBar(Model model , @ModelAttribute("qo")CustomerReportQuery qo){
       /* PageInfo pageInfo = customerReportService.selectCustomerReport(qo);
        //获取结果集,用map集合来存储,
        List<Map> list = pageInfo.getList();*/
       //上面的两个代码可以省略,因为不需要分页,我们建立一个listAll方法就可以了
        List<Map> list = customerReportService.listAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList<Object> xList = new ArrayList();
        //提供一个集合存储Y轴的数据
        ArrayList yList = new ArrayList();
        //需要把这些数据转为成echart需要结构,x轴和y轴的数据要分开
        for (Map map : list) {
            //获取的是列名的别名,因为有两个参数,获取两次
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }

        //共享页面到浏览器,如果不转换成String数据类型的格式的话报错,freemarker识别不出,所以要SON.toJSONString转换为string格式
        model.addAttribute("xList",JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "customerReport/listByBar";
    }

    @RequestMapping("/listByPie")
    public String listByPie(Model model , @ModelAttribute("qo")CustomerReportQuery qo){
        /*PageInfo pageInfo = customerReportService.selectCustomerReport(qo);
        //获取结果集,用map集合来存储,
        List<Map> list = pageInfo.getList();*/
        List<Map> list = customerReportService.listAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList<Object> xList = new ArrayList();
        //提供一个集合存储Y轴的数据
        ArrayList yList = new ArrayList();
        //需要把这些数据转为成echart需要结构,x轴和y轴的数据要分开
        for (Map map : list) {
            //获取的是列名的别名,因为有两个参数,获取两次
            xList.add(map.get("groupType"));
            //因为y轴的date是一个数组,所以我们要用map集合来存储数据
            HashMap<String, Object> hashMap = new HashMap<>();
            //[{value: 335, name: '孙总'}],孙总name是一个groupType列名,number是值value,所以还要创建一个groupType
            hashMap.put("name",map.get("groupType"));
            hashMap.put("value",map.get("number"));

            yList.add(hashMap);
        }

        //共享页面到浏览器,如果不转换成String数据类型的格式的话报错,freemarker识别不出,所以要SON.toJSONString转换为string格式
        model.addAttribute("xList",JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        //左上角标题的时候显示子查询的状态员工/年份
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));

        return "customerReport/listByPie";
    }
}
